package nimble.pay.trade.server.system.processor;

import nimble.pay.trade.server.framework.TradeProcessor;
import nimble.pay.trade.server.system.chain.trade.BeforeTradeProcessChain;
import nimble.pay.trade.server.system.data.TradeDataGenerator;
import nimble.pay.trade.server.system.data.TradeDataUpdator;
import nimble.pay.trade.specification.TradeObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author max woods
 * @date 2018/8/6
 */

@Component
public class ServerTradeProcessor implements TradeProcessor {

  private static final Logger logger = LoggerFactory.getLogger(ServerTradeProcessor.class);

  /**
   * 交易前置处理链
   */
  private BeforeTradeProcessChain beforeTradeProcessChain;

  private TradeDataGenerator tradeDataGenerator;

  private TradeDataUpdator tradeDataUpdator;

  @Autowired
  public ServerTradeProcessor(BeanFactory beanFactory) {
    beforeTradeProcessChain = beanFactory.getBean(BeforeTradeProcessChain.class);
    tradeDataGenerator = beanFactory.getBean(TradeDataGenerator.class);
    tradeDataUpdator = beanFactory.getBean(TradeDataUpdator.class);
  }

  /**
   * 处理交易
   *
   * @param request  交易请求
   * @param response 交易响应
   * @return 交易处理是否成功
   */
  @Override
  public boolean processTrade(TradeObject request, TradeObject response) {
    logger.info("=========交易处理开始========");
    // 交易前置处理
    if (!beforeTradeProcessChain.processTrade(request, response)) {
      logger.error("=========交易前置处理失败========");
      return false;
    }
    // 交易数据生成
    if (!tradeDataGenerator.processTrade(request, response)) {
      logger.error("=========交易数据生成失败========");
      return false;
    }
    // 交易路由前置处理
    // 交易路由
    // 交易路由后置处理
    // 交易数据更新
    if (!tradeDataUpdator.processTrade(request, response)) {
      logger.error("=========交易数据更新失败========");
      return false;
    }
    // 交易后置处理
    logger.info("=========交易处理成功========");
    return true;
  }

  /**
   * 交易数据生成
   *
   * @param request
   * @param response
   * @return
   */
  private boolean tradeDataGenerate(TradeObject request, TradeObject response) {
    logger.info("=========交易数据生成========");
    return true;
  }

  /**
   * 交易数据更新
   *
   * @param request
   * @param response
   * @return
   */
  private boolean tradeDataUpdate(TradeObject request, TradeObject response) {
    logger.info("=========交易数据更新========");
    return true;
  }
}